﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using SilverlightApplication;
using System.Globalization;

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {

        public Page()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
            upDateCities("uk");
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Browser.HtmlPage.RegisterScriptableObject("MySilverlightObject", this);
        }

        [ScriptableMember]
        public void upDateCities(string strCountry)
        {
            ServiceReference1.Service1SoapClient client = new SilverlightApplication1.ServiceReference1.Service1SoapClient();
            client.GetCityDataCompleted += new EventHandler<SilverlightApplication1.ServiceReference1.GetCityDataCompletedEventArgs>(client_GetCityDataCompleted);
            client.GetCityDataAsync(strCountry);
        }


        private List<CityData> getCities(string strCountry)
        {
            List<CityData> ret = new List<CityData>();
            switch (strCountry)
            {
                case "france":
                    {
                        ret.Add(new CityData("Paris", 48.87, 2.33));
                        ret.Add(new CityData("Lourdes", 43.1, 0.05));
                        ret.Add(new CityData("Toulouse", 43.6, 1.38));
                        break;
                    }
                case "uk":
                    {
                        ret.Add(new CityData("London", 51.5, 0));
                        ret.Add(new CityData("Stratford-Upon-Avon", 52.3, -1.71));
                        ret.Add(new CityData("Edinburgh", 55.95, -3.16));
                        break;
                    }
                case "germany":
                    {
                        ret.Add(new CityData("Berlin", 52.52, 13.42));
                        ret.Add(new CityData("Munich", 48.13, 11.57));
                        ret.Add(new CityData("Hamburg", 53.58, 9.98));
                        break;
                    }
            }


            #region Dynamic

            ServiceReference1.Service1SoapClient client = new SilverlightApplication1.ServiceReference1.Service1SoapClient();
            client.GetCityDataCompleted += new EventHandler<SilverlightApplication1.ServiceReference1.GetCityDataCompletedEventArgs>(client_GetCityDataCompleted);
            client.GetCityDataAsync("Thailand");
            
            
            #endregion Dynamic

            return ret;
        }

        void client_GetCityDataCompleted(object sender, SilverlightApplication1.ServiceReference1.GetCityDataCompletedEventArgs e)
        {
            //List<SilverlightApplication.CityData> cities = new List<SilverlightApplication.CityData>();
            //foreach (var i in e.Result)
            //{
            //    cities.Add(
            //            new SilverlightApplication.CityData(i.CityName, i.Latitude, i.Longitude)
            //        );
            //}
            itmCities.ItemsSource = e.Result;
            
            
          


        }

        private void TextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            String strCity = "";
            String strCountry = "";
            TextBlock clickedText = sender as TextBlock;
            strCity = clickedText.Text;
            TextBlock title = this.FindName("titleText") as TextBlock;
            if (title != null)
            {
                strCountry = title.Text;
            }
            string toFind = strCity + "," + strCountry;
            ScriptObject sMoveMap = (ScriptObject)HtmlPage.Window.GetProperty("MoveMap");
            sMoveMap.InvokeSelf(toFind);
        }

    }
}
